package userInterface;

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;

import bancodedados.Database;
import bancodedados.LoginResultSetDataFetcher;

public class LoginWindow extends JFrame implements ActionListener{
	private static final long	serialVersionUID	= 1353773662294162039L;
	
	private static LoginWindow frame;
	
	private JPanel contentPane;
	private JTextField textField;
	private JPasswordField passwordField;
	private JLabel lblSenha;
	private JLabel lblCasa;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					getInstance().setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public LoginWindow() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		textField = new JTextField();
		textField.setColumns(10);
		
		passwordField = new JPasswordField();
		
		lblSenha = new JLabel("Senha:");
		
		lblCasa = new JLabel("CASA");
		lblCasa.setFont(new Font(Font.SERIF, Font.ITALIC, 32));
		
		JButton btnEntrar = new JButton("Entrar");
		btnEntrar.addActionListener(this);
		
		JLabel lblUsurio = new JLabel("Usu\u00E1rio:");
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(134)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addComponent(lblUsurio)
							.addContainerGap())
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_contentPane.createSequentialGroup()
									.addComponent(lblSenha)
									.addPreferredGap(ComponentPlacement.RELATED, 59, GroupLayout.PREFERRED_SIZE))
								.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING, false)
									.addComponent(passwordField, Alignment.LEADING)
									.addComponent(textField, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 125, Short.MAX_VALUE)))
							.addGap(44))))
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(165)
					.addComponent(btnEntrar)
					.addContainerGap(196, Short.MAX_VALUE))
				.addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
					.addGap(158)
					.addComponent(lblCasa, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
					.addContainerGap())
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.TRAILING)
				.addGroup(Alignment.LEADING, gl_contentPane.createSequentialGroup()
					.addGap(33)
					.addComponent(lblCasa)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(lblUsurio)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addComponent(lblSenha)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(passwordField, GroupLayout.DEFAULT_SIZE, 20, Short.MAX_VALUE)
					.addGap(18)
					.addComponent(btnEntrar)
					.addGap(27))
		);
		contentPane.setLayout(gl_contentPane);
	}

	/**
	 * Faz o login
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		LoginResultSetDataFetcher fetcher = new LoginResultSetDataFetcher(this.textField.getText(), new String(this.passwordField.getPassword()));
		try {
			Database.getInstance().doSelect(fetcher);
			if (fetcher.hasResults()){
				if (fetcher.getFirstResult() instanceof RH){
					new RHWindow().setVisible(true);
				}
				else if (fetcher.getFirstResult() instanceof Atendente){
					new MainWindow().setVisible(true);
				}
				this.setVisible(false);
			}
		} catch (SQLException e1) {
			
		}
	}

	public static Window getInstance() {
		if (frame == null){
			frame = new LoginWindow();
		}
		return frame;
	}
}
